#ifndef GAME_H
#define GAME_H

#include <QWidget>
#include <QPainter>
#include <QMouseEvent>
#include <QRadioButton>
#include <QEventLoop>
#include <QTimer>
#include "chessman.h"
#include "enums.h"
#include <vector>
#include <limits> // for std::numeric_limits
#include <QLCDNumber>

namespace Ui {
class game;
}

class game : public QWidget
{
    Q_OBJECT

public:
    explicit game(QWidget *parent = nullptr);
    ~game();
    const int WIDTH = 40;
    const int LINES = 19;             // 19*19
    const int SIZE = LINES + 1;
    const int chessmanoffset = 2 * WIDTH;
    const float selectXoffset = 13.7;
    const int selectY = 253;
    const int Start = 20;
    const int yChessman = 260;
    chessman* table = nullptr;
    void initui();
    void initgame();
    void initpainter();
    void paintEvent(QPaintEvent *);
    void mouseMoveEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent* event);
    void print(int x, int y);
    bool erase(int x, int y);
    int iswin();
    chessman get(int x, int y);
    int put(int x, int y, chessmanType type);
    void delay_msec(unsigned int msec);
    void msg(QString q);
    void clearmsg();
    int step = 0;
    std::vector<int> stepX;
    std::vector<int> stepY;
    int chessPlayer;
    int gamemode = started;
    void setStep(int step);
    void makeAIMove();
    int minimax(int depth, int alpha, int beta, bool isMaximizingPlayer);
    bool isAliveFour(int x, int y);


private slots:
    void on_pushButton_3_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_clicked();
    void restartgame(bool changeOffensiveMovePlayer);
    void updateCountdown(); // 更新倒计时
    void startCountdown();  // 启动倒计时

private:
    Ui::game *ui;
    QPainter *painter;
    bool drawChess = false;
    int mouseX, mouseY;
    int winner = none_win;
    int chessmanNum = 0;
    int putSucess;

private:
    QTimer *countdownTimer;
    QLCDNumber *countdownDisplay;
    int countdownTime; // 倒计时的初始时间（秒）

};

#endif // GAME_H
